#ifdef _DEBUG
#include <iostream>
using namespace std;
#endif

#include "TriCirculator.h"
#include "TriVertex.h"
#include "TriEdge.h"


//////////////////////////////////////////////////////////////////////////////
// VertexCirculator functions
//////////////////////////////////////////////////////////////////////////////

bool VertexCirculator::HasBase(void)
{
   return base!=NULL;
}

void VertexCirculator::SetBase(const TriVertex* v)
{
   base=v->AdjEdge();
   Start();
#ifdef _DEBUG
   vertex=(TriVertex*)v;
#endif
}

//Advance clockwise.
//Returns false if it reaches a boundary
bool VertexCirculator::AdvanceCW(void)
{
   //Check if we reached a boundary
   if(now->Twin()==NULL)
   {
      CW=false;
      if(second)
      {
         done=true;
      }
      else
      {
         CCW=true;
      }
      return false;
   }

   //Advance
   TriEdge *next=now->Twin()->Next();

   //Check if we reached the beginning
   if(next==base)
   {
      done=true;
   }
   else
   {
      now=next;
      valid=now;
   }
   return true;
}

//Advance counterclockwise.
//Returns false if it reaches a boundary.
bool VertexCirculator::AdvanceCCW(void)
{
   //Check if we reached a boundary
   if(now->Prev()->Twin()==NULL)
   {
      CCW=false;
      if(second)
      {
         done=true;
      }
      else
      {
         CW=true;
      }
      return false;
   }

   //Advance
   TriEdge *next=now->Prev()->Twin();

   //Check if we reached the beginning
   if(next==base)
   {
      done=true;
   }
   else
   {
      now=next;
      valid=now;
   }
   return true;
}


//////////////////////////////////////////////////////////////////////////////
// CWVertexCirculator functions
//////////////////////////////////////////////////////////////////////////////

void CWVertexCirculator::Advance(void)
{
   if(IsDone()) return;

   if(CW)
   {
      if(!AdvanceCW())
      {
         now=base;
         second=true;
         AdvanceCCW();
      }
   }
   else
   {
      AdvanceCCW();
   }

#ifdef _DEBUG
   if(vertex->ValenceComputed() && ++iter > vertex->Valence())
   {
      cerr << "WARNING!! " << iter << 
         " hops circulating around a vertex (id " << vertex->Id() << ") of valence " << vertex->Valence() << endl;
      done=true;
   }
#endif

}


//////////////////////////////////////////////////////////////////////////////
// CCWVertexCirculator functions
//////////////////////////////////////////////////////////////////////////////

void CCWVertexCirculator::Advance(void)
{
   if(IsDone()) return;

   if(CCW)
   {
      if(!AdvanceCCW())
      {
         now=base;
         second=true;
         AdvanceCW();
      }
   }
   else
   {
      AdvanceCW();
   }

#ifdef _DEBUG
   if(vertex->ValenceComputed() && ++iter > vertex->Valence())
   {
      cerr << "WARNING!! " << iter << 
         " hops circulating around a vertex of valence " << vertex->Valence() << endl;
      done=true;
   }
#endif

}

